<?php

/**
 * @author игорь
 * @copyright 2009
 */

class CommentWizard
{
	var $db,$user;
	var $table = "comments";
	var $id = "id";
	var $pid = "pid";
	function __construct($tablename = "")
	{
		global $db,$user;
		$this->db = $db;
		$this->user = $user;
		if(isset($tablename) and strval($tablename) != '')
		{
			$this->table = $tablename;
		}
	}
	function getCommentsTree($module,$obj_id,$pid = 0,$level_limiter = 0,$status = 2,$level = 0)
	{
		// Проверка на фатальные ошибки
		if(isset($module) and strval($module) != '')
		{
			$comment_module = mysql_real_escape_string($module);
		}
		else return false;
		if(isset($obj_id) and intval($obj_id) != 0)
		{
			$comment_obj_id = intval($obj_id);
		}
		else return false;
		// присваиваем поступившие переменные внутренним членам функции
		if(isset($selected) and (is_array($selected) or is_int($selected)))
		{
			$selected_items = true;
		}
		else $selected_items = false;
		$level_limiter = intval($level_limiter);
		$status = intval($status);
		$parent = intval($pid);
		$sub = array();
		$inside_selected = false;
		/** Query for comments list **/
		$query = "
		SELECT 
			par.$this->id,par.$this->pid,par.theme,par.nickname,par.comment,par.time,par.status,par.module,par.obj_id,par.ip,COUNT(DISTINCT ch.id) AS childs 
		FROM $this->table AS par 
		LEFT JOIN $this->table AS ch ON (par.$this->id = ch.$this->pid AND ch.status >='$status') 
		WHERE 
			par.module = '$comment_module' AND par.obj_id = '$comment_obj_id' AND par.$this->pid = '$parent' AND par.status>='$status' 
		GROUP BY par.id 
		ORDER BY par.id";
		$rows = $this->db->fetch_array($query);
		//print_var($rows);
		if($rows != false)
		{
			$i = 0;
			foreach ($rows as $k=>$row)
			{
				$sub[$i][$this->id] = $row[$this->id];
				if ($selected_items)
				{
		            if (is_array($selected) and in_array($row[$this->id], $selected))
		    		{
		    			$sub[$i]['selected'] = 'selected';
		    			$inside_selected = true;
		    		}
		    		elseif (is_int($selected) and intval($selected) == $row[$this->id])
		    		{
		    			$sub[$i]['selected'] = 'selected';
		    			$inside_selected = true;
		    		}
		        }
		        else
		        {
		    	}
				$sub[$i] = $row;
				$next_level = $level + 1;
				$sub[$i]['level'] = $next_level;
		
				if($level_limiter == 0 or $next_level <= $level_limiter)
				{
					if ($sub[$i]['childs']>0)
					{
			            $subnodes = $this->getCommentsTree($module,$obj_id,$row[$this->id],$level_limiter,$status,$next_level);
						$sub[$i]['sub']=$subnodes['sub'];
						if($subnodes['inside_selected'] == true)
						{
							$sub[$i]['inside_selected'] = true;
							$inside_selected = true;
						}
					}
				}
				$i++;
			}
		}
		return array('sub'=>$sub,'inside_selected'=>$inside_selected);

	}
	function setRandomNumbers()
	{
	    if (isset($_SESSION['control_summ']))
		{
			unset ($_SESSION['control_summ']);
			unset ($_SESSION['num1']);
			unset ($_SESSION['num2']);
		}
		$_SESSION['random_result'] = rand(1, 2);
		switch($_SESSION['random_result']){
	        case 1:
	        $_SESSION['num1'] = rand (1, 9);
	        $_SESSION['num2'] = rand (1, 9);
	        $_SESSION['control_summ'] = $_SESSION['num1'] + $_SESSION['num2'];
	        break;
	        case 2:
	        $_SESSION['num1'] = rand (1, 9);
	        $_SESSION['num2'] = rand (1, 9);
	        $_SESSION['control_summ'] = $_SESSION['num1'] * $_SESSION['num2'];
	        break;
	    }
        return $_SESSION['control_summ'];
	}
}
/** Структура таблицы комментариев */
/*
id
pid
module
obj_id
ip
comment
nickname
theme
time
*/
?>